package com.lifo.shoppingcart.controller;

import com.lifo.shoppingcart.bean.HfShoppingCart;
import com.lifo.shoppingcart.service.ShoppingCartManagerService;
import com.lifo.utils.common.date.DateUtil;
import com.lifo.utils.wrapper.ResponseMsgBean;
import com.lifo.utils.wrapper.WrapperJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2018/6/27 0027.
 */
@Controller
@Slf4j
public class ManagerShoppingCartController {


    @Autowired
    private ShoppingCartManagerService shoppingCartManagerService;



    @RequestMapping(value = "/cart/saveShoppingCart" )
    public  @ResponseBody  String saveShoppingCart(HfShoppingCart shoppingCart,HttpServletRequest request){
        HfShoppingCart hfShoppingCart = new HfShoppingCart();
        BeanUtils.copyProperties(shoppingCart,hfShoppingCart);
        hfShoppingCart.setCreateDate(DateUtil.getNowDateString("yyyy-MM-dd"));
        hfShoppingCart.setCreateTime(DateUtil.getNowDateString());
        try {
            log.debug("加入商品到购物车,商品信息为==>{}",new WrapperJson().toJson(hfShoppingCart));
            shoppingCartManagerService.saveShoppingCart(hfShoppingCart);

            int goodCount =  shoppingCartManagerService.countByUserId(hfShoppingCart.getUserId());
            request.getSession().setAttribute("shoppingCartGoodCount",goodCount);

            return ResponseMsgBean.getMsgJsonWithSuccess();
        }catch (Exception e){
            log.error("加入商品到购物车失败,信息==>{}",e.getMessage());
            e.printStackTrace();
            return ResponseMsgBean.getMsgJsonWithSystemError();
        }

    }
}
